package com.bbzn.device.client.controller;

import com.bbzn.device.client.vo.req.RangeEntity;
import com.bbzn.device.client.aspect.OperationAnnotation;
import com.bbzn.device.client.constant.DeviceConstant;
import com.bbzn.device.client.constant.MongoDbConstant;
import com.bbzn.device.client.constant.TimeConstant;
import com.bbzn.device.client.dataobject.LocusCoordinate;
import com.bbzn.device.client.dataobject.SystemParam;
import com.bbzn.device.client.dto.time.TimePageDTO;
import com.bbzn.device.client.exception.DefinitException;
import com.bbzn.device.client.service.CommonService;
import com.bbzn.device.client.service.SimpleMongoService;
import com.bbzn.device.client.service.SystemParamByCacheService;
import com.bbzn.device.client.utils.*;
import com.bbzn.device.client.utils.date.DateUtil;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author wulongbo
 * @since 2021-04-23
 */
@RestController
@RequestMapping("/locusCoordinate")
@Api(value = "LocusCoordinate", tags = "轨迹坐标上传")
public class LocusCoordinateController extends BaseApiService {

    @Autowired
    private CommonService commonService;

    @Autowired
    private SystemParamByCacheService systemParamByCacheService;

    @Autowired
    private SimpleMongoService simpleMongoService;

    /**
     * APP根据日期获取坐标集合
     *
     * @param reqVO
     * @return
     */
    @RequestMapping(value = "/getLocusByDate", method = RequestMethod.POST)
    @OperationAnnotation(content = "APP根据日期获取坐标集合")
    public BaseResponse getLocusByDate(@RequestBody TimePageDTO reqVO, HttpServletRequest request) throws Exception {
        SystemParam systemParam = null;
        String secretKey = commonService.getWebSalt(request);
        String language = MayiktJwtUtils.getLanguage(request, secretKey);
        if (!OptionalBean.ofNullable(String.valueOf(reqVO.getStartTime())).isPresent()) {
            systemParam = systemParamByCacheService.getValues(TimeConstant.TIME_ENUM, TimeConstant.TIME_NO_START_TIME_PREFIX + language);
            throw new DefinitException(systemParam.getCode(), systemParam.getMassage());
        }
        if (!OptionalBean.ofNullable(String.valueOf(reqVO.getEndTime())).isPresent()) {
            systemParam = systemParamByCacheService.getValues(TimeConstant.TIME_ENUM, TimeConstant.TIME_NO_END_TIME_PREFIX + language);
            throw new DefinitException(systemParam.getCode(), systemParam.getMassage());
        }
        if (!OptionalBean.ofNullable(reqVO.getImei()).isPresent()) {
            systemParam = systemParamByCacheService.getValues(DeviceConstant.DEVICE_ENUM, DeviceConstant.DEVICE_NO_IMEI_PREFIX + language);
            throw new DefinitException(systemParam.getCode(), systemParam.getMassage());
        }
        /*if (reqVO.getPageNum() == null) {
            reqVO.setPageNum(1);
        }
        if (reqVO.getPageSize() == null) {
            reqVO.setPageSize(50);
        }*/
        Date startTime = DateUtil.stringToDate(reqVO.getStartTime(),"yyyy-MM-dd HH:mm:ss");
        Date endTime = DateUtil.stringToDate(reqVO.getEndTime(),"yyyy-MM-dd HH:mm:ss");
        Map<String, RangeEntity> map = new HashMap<>();
        RangeEntity rangeEntity = new RangeEntity();
        rangeEntity.setGte(startTime.getTime());
        rangeEntity.setLt(endTime.getTime());
        map.put("createTime", rangeEntity);
        Map<String, Object> conditions = new HashMap<>();
        conditions.put("imei", reqVO.getImei());
        List<LocusCoordinate> page = simpleMongoService.selectByCondition(MongoDbConstant.LOCUS_COORDINATE, map, conditions,null, LocusCoordinate.class, reqVO.getPageNum(), reqVO.getPageSize());
        Map<String, List<LocusCoordinate>> data = new HashMap<>();
        data.put("resultList",page);
        return setResultSuccess(data);
    }
}
